<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录 - 列车车次管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .login-container {
            min-height: 100vh;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .login-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body>
<div class="login-container d-flex align-items-center">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-6 col-lg-4">
                <div class="login-card">
                    <div class="card-body p-5">
                        <div class="text-center mb-4">
                            <i class="fas fa-train fa-3x text-primary mb-3"></i>
                            <h3 class="fw-bold">列车车次管理系统</h3>
                            <p class="text-muted">请登录您的账户</p>
                        </div>

                        <!-- 错误消息 -->
                        <div th:if="${error}" class="alert alert-danger alert-dismissible fade show" role="alert">
                            <i class="fas fa-exclamation-triangle me-2"></i>
                            <span th:text="${error}">错误信息</span>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>

                        <!-- 成功消息 -->
                        <div th:if="${message}" class="alert alert-success alert-dismissible fade show" role="alert">
                            <i class="fas fa-check-circle me-2"></i>
                            <span th:text="${message}">成功信息</span>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>                        <form method="post" action="/perform_login" id="loginForm">
                            <input type="hidden" id="redirectInput" name="redirect" value="">
                            <div class="mb-3">
                                <label for="username" class="form-label">
                                    <i class="fas fa-user me-1"></i>用户名
                                </label>
                                <input type="text" class="form-control" id="username" name="username"
                                       placeholder="请输入用户名" required>
                            </div>
                            <div class="mb-3">
                                <label for="password" class="form-label">
                                    <i class="fas fa-lock me-1"></i>密码
                                </label>
                                <input type="password" class="form-control" id="password" name="password"
                                       placeholder="请输入密码" required>
                            </div>
                            <div class="mb-3 form-check">
                                <input type="checkbox" class="form-check-input" id="rememberMe" name="remember-me">
                                <label class="form-check-label" for="rememberMe">记住我</label>
                            </div>
                            <div class="d-grid mb-3">
                                <button type="submit" class="btn btn-primary btn-lg">
                                    <i class="fas fa-sign-in-alt me-1"></i>登录
                                </button>
                            </div>
                        </form>

                        <div class="text-center">
                            <p class="mb-0">还没有账号? <a href="/register" class="text-decoration-none">立即注册</a></p>
                            <a href="/" class="text-muted text-decoration-none">
                                <i class="fas fa-arrow-left me-1"></i>返回首页
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 页面加载时获取URL参数中的redirect值
    window.addEventListener('DOMContentLoaded', function() {
        const urlParams = new URLSearchParams(window.location.search);
        const redirectParam = urlParams.get('redirect');
        if (redirectParam) {
            document.getElementById('redirectInput').value = redirectParam;
        }
        
        // 处理登录表单提交
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            
            fetch('/perform_login', {
                method: 'POST',
                body: formData
            })
            .then(response => {
                if (response.ok) {
                    // 登录成功，检查用户角色并重定向
                    return fetch('/api/users/current', {
                        method: 'GET',
                        credentials: 'same-origin'
                    });
                } else {
                    throw new Error('登录失败');
                }
            })
            .then(response => response.json())
            .then(user => {
                // 根据用户角色和重定向参数决定跳转
                const redirectUrl = document.getElementById('redirectInput').value;
                
                if (user.userRole === 'ADMIN' && !redirectUrl) {
                    // 管理员且没有指定重定向，跳转到管理后台
                    window.location.href = '/admin/dashboard';
                } else if (redirectUrl) {
                    // 有重定向参数，跳转到指定页面
                    window.location.href = redirectUrl;
                } else {
                    // 普通用户，跳转到首页
                    window.location.href = '/';
                }
            })
            .catch(error => {
                console.error('登录处理失败:', error);
                // 登录失败，刷新页面显示错误信息
                window.location.reload();
            });
        });
    });
</script>
</body>
</html>